layui.use(['form','jquery','layer'], function () {
    var form   = layui.form;
    var $      = layui.jquery;
    var layer  = layui.layer;
    var phone = $('#phone');
    var username = $('#username');
    var password1 = $('#password');
    var rpwd = $('#rpwd');


    //添加表单失焦事件

    //验证手机号
    $('#phone').blur(function () {
        var pattren = /^1[345789]\d{9}$/;
        if(!$(this).val().match(pattren)){
            $('#swr').removeAttr('hidden');
            $('#sri').attr('hidden','hidden');
            layer.msg('请输入合法手机号', {icon: 5});

        }else {
                        $('#sri').removeAttr('hidden');
                        $('#swr').attr('hidden','hidden');
                       phone.addClass("phone_success");
                }
            });




    //验证用户名
    $('#username').blur(function() {
        var user = $(this).val();

        //alert(user);
        $.ajax({
            url:'/user/usernameCheck',
            type:'post',
            dataType:'json',
            data:{username:user},
            //验证用户名是否可用
            success:function(data){
                if (data.status == 200) {
                    $('#ri').removeAttr('hidden');
                    $('#wr').attr('hidden','hidden');
                    username.addClass("username_success");

                } else if(data.status == 4){
                    $('#wr').removeAttr('hidden');
                    $('#ri').attr('hidden','hidden');
                    layer.msg('用户名不能为空', {icon: 5})

                } else {
                    $('#wr').removeAttr('hidden');
                    $('#ri').attr('hidden','hidden');
                    layer.msg('当前用户名已被占用! ', {icon: 5})
                }

            }
        })

    });

    // you code ...
    // 为密码添加正则验证
    $('#password').blur(function() {
        var reg = /^[\w]{6,12}$/;
        if(!($('#password').val().match(reg))){
            //layer.msg('请输入合法密码');
            $('#pwr').removeAttr('hidden');
            $('#pri').attr('hidden','hidden');
            layer.msg('请输入合法密码', {icon: 5});
            password.removeClass("password_success");
        }else {
            $('#pri').removeAttr('hidden');
            $('#pwr').attr('hidden','hidden');
           password1.addClass("password_success");

        }
    });

    //验证两次密码是否一致
    $('#rpwd').blur(function() {
        if($('#password').val() != $('#rpwd').val()){
            $('#rpwr').removeAttr('hidden');
            $('#rpri').attr('hidden','hidden');
            layer.msg('两次输入密码不一致!', {icon: 5});
            rpwd.removeClass("rpwd_success");
        }else {
            $('#rpri').removeAttr('hidden');
            $('#rpwr').attr('hidden','hidden');
            rpwd.addClass("rpwd_success");
        };
    });

});